package com.lysj.bhp.borrowing.controller;

import com.lysj.bhp.borrowing.base.BorrowingContent;
import com.lysj.bhp.borrowing.domain.BorrowingRate;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/14--9:32
 * @Description: 借贷利率后台管理
 */
@RestController
@RequestMapping("admin/borrowing_rate")
public class BorrowingRateController extends BorrowingContent {

    @RequestMapping("/list")
    public Resp list(BorrowingRate model) {
        model.setIsExist(1);
        Specification specification = ParamUtil.createSpecification(model);
        List<BorrowingRate> borrowingRates = borrowingRateRepository.findAll(specification, new Sort(Sort.Direction.DESC, "repaymentWay"));
        return new Resp(borrowingRates);
    }

    @PostMapping("/add")
    public Resp add(BorrowingRate model) {
        //参数判断部分开始
        if (ParamUtil.isBlack(model.getRateType())) {
            return new Resp(Resp.PARAM_ERROR, "请选择的利率类型！");
        }
        if (ParamUtil.isBlack(model.getRepaymentWay())) {
            return new Resp(Resp.PARAM_ERROR, "请选择的还款方式！");
        }
        if (ParamUtil.isBlack(model.getBorrowingRate())) {
            return new Resp(Resp.PARAM_ERROR, "请填写利率");
        }
        if (model.getBorrowingRate().compareTo(new BigDecimal("40")) >= 1 || model.getBorrowingRate().compareTo(new BigDecimal("15")) <= -1) {
            return new Resp(Resp.PARAM_ERROR, "设置失败！利率只能设置在15%~40之间");
        }
        //参数判断部分结束
        BorrowingRate borrowingRate = borrowingRateRepository.findByRateTypeAndRepaymentWayAndIsExist(model.getRateType(), model.getRepaymentWay(), 1);
        if (borrowingRate != null) {
            return new Resp(Resp.PARAM_ERROR, "该利率已经存在，添加失败");
        }
        model.setIsExist(1);
        borrowingRateRepository.save(model);
        //若有利率记录本处于删除状态，则修改其状态变为正常,更新利率
        borrowingRate = borrowingRateRepository.findByRateTypeAndRepaymentWayAndIsExist(model.getRateType(), model.getRepaymentWay(), 2);
        if (borrowingRate != null) {
            borrowingRate.setIsExist(1);
            borrowingRate.setBorrowingRate(model.getBorrowingRate());
            borrowingRateRepository.save(borrowingRate);
        }
        //若不存在，则创建新的记录类型;
        else {

        }
        return new Resp("添加成功！");
    }

    @PostMapping("/edit")
    public Resp edit(Long borrowingRateId,BigDecimal borrowingRate) {
        //判断开始
        if (ParamUtil.isBlack(borrowingRateId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择需要修改的利率记录");
        }
        if(null==borrowingRate){
            return new Resp(Resp.PARAM_ERROR,"利率不能为空");
        }
        if (borrowingRate.compareTo(new BigDecimal(BigInteger.ZERO)) <= -1) {
            return new Resp(Resp.PARAM_ERROR, "利率不能为负数");
        }
        //查找该ID，状态为正常的利率记录
        BorrowingRate borrowingRateChange = borrowingRateRepository.findByBorrowingRateIdAndIsExist(borrowingRateId, 1);
        if (borrowingRateChange == null) {
            return new Resp(Resp.PARAM_ERROR, "该记录数据不存在！请联系管理员");
        }
        //修改利率
        if (borrowingRate.compareTo(new BigDecimal("40")) >= 1 || borrowingRate.compareTo(new BigDecimal("15")) <= -1) {
            return new Resp(Resp.PARAM_ERROR, "设置失败！利率只能设置在15%~40%之间");
        }
        borrowingRateChange.setBorrowingRate(borrowingRate);
        borrowingRateRepository.save(borrowingRateChange);
        return new Resp("修改利率成功！");
    }

    @PostMapping("/remove")
    public Resp remove(BorrowingRate model) {
        //判断开始
        if (model.getBorrowingRateId() == null || ParamUtil.isBlack(model.getBorrowingRateId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择需要修改的利率记录");
        }
        //查找该ID，状态为正常的利率记录
        BorrowingRate borrowingRate = borrowingRateRepository.findByBorrowingRateIdAndIsExist(model.getBorrowingRateId(), 1);
        if (borrowingRate == null) {
            return new Resp(Resp.PARAM_ERROR, "该记录数据不存在！请联系管理员");
        }
        borrowingRate.setIsExist(2);
        borrowingRateRepository.save(borrowingRate);
        return new Resp("删除利率记录成功！");
    }
}
